package club.zhcs.captrue.module.image;

import javax.servlet.http.HttpServletRequest;

import org.nutz.castor.Castors;
import org.nutz.dao.entity.Record;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import club.zhcs.capture.bean.Material;
import club.zhcs.capture.bean.Material.Type;
import club.zhcs.capture.bean.auth.User;
import club.zhcs.capture.biz.img.IMaterialService;
import club.zhcs.capture.ocr.BaiduOcr;
import club.zhcs.capture.ocr.BaiduOcr.IDCard;
import club.zhcs.capture.qr.QRCode;
import club.zhcs.capture.vo.Consts.PermissionGroup;
import club.zhcs.capture.vo.Consts.SessionKeys;

import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.utils.entries.Result;
import com.dgj.utils.file.Images;
import com.dgj.utils.ua.UserAgent;
import com.dgj.utils.web.pager.Pager;

@At("/image")
public class ImageModule extends AbstractBaseModule {

	@Inject
	IMaterialService materialService;

	@Override
	public String _getNameSpace() {
		return PermissionGroup.IMAGE_ADMIN.getNameSpace();
	}

	@At
	@Ok("vel:image/list.html")
	public Result list(int page, @Attr(SessionKeys.SESSION_USER_KEY) User user) {
		page = _fixPage(page);
		Pager<Record> pager = materialService.listByPage(page, user.getId());
		pager.setUrl(_base() + "/image/list");
		return Result.success().addData("pager", pager);
	}

	@At
	@Ok("vel:image/list.html")
	public Result search(int page, String key, @Attr(SessionKeys.SESSION_USER_KEY) User user) {
		page = _fixPage(page);
		key = _fixSearchKey(key);
		Pager<Record> pager = materialService.search(page, key, user.getId());
		pager.setUrl(_base() + "/image/list");
		return Result.success().addData("pager", pager).addData("key", key);
	}

	@At("/preview/*")
	@Ok("vel:image/preview.html")
	public Result preview(String uuid) {

		return Result.success().addData("images", materialService.listByUUID(uuid));
	}

	@At
	@Ok("vel:image/gather.html")
	public Result gather(HttpServletRequest request) {
		UserAgent uaAgent = new UserAgent(request.getHeader("User-Agent"));
		NutMap errors = NutMap.NEW();
		if (uaAgent.getBrowser().toString().indexOf("IE") < 0) {
			errors.put("browser", "oxc控件不支持非IE浏览器,请在ie浏览器下使用数据采集功能");
		}
		if (uaAgent.getOperatingSystem().toString().indexOf("WIN") < 0) {
			errors.put("os", "oxc控件不支持非windows系统");
		}
		int[] numbers = new int[100];
		for (int i = 0; i < numbers.length; i++) {
			numbers[i] = i + 1;
		}

		Type type = Type.FINANCE;
		String docType = _getCookie("docType");
		if (!Strings.isBlank(docType)) {
			type = Castors.me().castTo(docType, Type.class);
		}
		return Result.success().addData("errors", errors).addData("oxcSource", _base() + "/resources/oxc/DoccameraOcx.exe").addData("types", Material.Type.values())
				.addData("numbers", numbers).addData("type", type);

	}

	@At
	@Ok("vel:image/idcard.html")
	public Result idcard(HttpServletRequest request) {
		UserAgent uaAgent = new UserAgent(request.getHeader("User-Agent"));
		NutMap errors = NutMap.NEW();
		if (uaAgent.getBrowser().toString().indexOf("IE") < 0) {
			errors.put("browser", "oxc控件不支持非IE浏览器,请在ie浏览器下使用数据采集功能");
		}
		if (uaAgent.getOperatingSystem().toString().indexOf("WIN") < 0) {
			errors.put("os", "oxc控件不支持非windows系统");
		}
		int[] numbers = new int[100];
		for (int i = 0; i < numbers.length; i++) {
			numbers[i] = i + 1;
		}

		Type type = Type.FINANCE;
		String docType = _getCookie("docType");
		if (!Strings.isBlank(docType)) {
			type = Castors.me().castTo(docType, Type.class);
		}
		return Result.success().addData("errors", errors).addData("oxcSource", _base() + "/resources/oxc/DoccameraOcx.exe").addData("types", Material.Type.values())
				.addData("numbers", numbers).addData("type", type);

	}

	@At
	public Result discriminate(String data) {
		if (Strings.isBlank(data)) {
			return Result.fail("没有图像数据");
		}
		if (data.indexOf("base64") >= 0) {
			data = data.substring(data.indexOf("base64") + 7);
		}
		try {
			String code = QRCode.from(Images.GeneratePngImage(data));
			return Result.success().addData("code", code);
		} catch (Exception e) {
			return Result.fail("识别失败,请填入二维码编号");
		}
	}

	@At
	public Result upload(@Param("..") NutMap data, @Param("card") IDCard card, @Attr(SessionKeys.SESSION_USER_KEY) User user) {
		data.put("userId", user.getId());
		String docType = data.getString("docType");
		_addCookie("docType", docType, Integer.MAX_VALUE);
		return materialService.uploadImg(data, card);
	}

	@At
	public Result baiduOcr(@Param("img") String img) {
		NutMap ocrResult = BaiduOcr.ocrBase64(img);
		if (ocrResult.getInt("errNum") == 0) {// 成功
			System.err.println(ocrResult);
			IDCard card = BaiduOcr.toCard(ocrResult);
			return Result.success().addData("card", card);
		}
		return Result.fail(ocrResult.getString("errMsg"));
	}

}
